package com.nwpu.coursesupport.view.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.nwpu.coursesupport.controller.AbstractController;
import com.nwpu.coursesupport.entity.bean.JSONResult;
import com.nwpu.coursesupport.exception.GenGradeException;
import com.nwpu.coursesupport.service.GenGradeService;
import com.nwpu.coursesupport.service.param.CalculateGradeParam;
import com.nwpu.coursesupport.service.param.GenGradeServiceParam;
import com.nwpu.coursesupport.service.param.IDParam;
import com.nwpu.coursesupport.tool.JSONUtils;
import com.nwpu.coursesupport.view.CourseGradeRateView;

@Controller
@RequestMapping("/gradeView")
public class GenGradeController extends AbstractController{

	@Autowired
	private GenGradeService genGradeService;
	
	@RequestMapping("/courseRate")
	public String courseRate(@RequestParam("courseId")int courseId,HttpServletRequest request){
		JSONResult rst = new JSONResult();
		rst.fail();
		IDParam p=new IDParam();
		p.setId(courseId);
		try{
			rst = this.genGradeService.getCourseRate(p);
		}catch(Exception e){
			logger.error("出现异常"+e.getMessage());
			rst.fail("后台出错");
		}
		request.setAttribute("data",this.parseData(rst));
		return "/teapage/courseScoreProportion";
	}
	
	@RequestMapping("/ajaxCourseRate")
	@ResponseBody
	public JSONResult ajaxCourseRate(@RequestParam("courseId")int courseId){
		JSONResult rst = new JSONResult();
		rst.fail();
		IDParam p=new IDParam();
		p.setId(courseId);
		try{
			rst = this.genGradeService.getCourseRate(p);
		}catch(Exception e){
			logger.error("出现异常"+e.getMessage());
			return rst.fail("后台出错");
		}
		return rst;
	}
	
	@RequestMapping(value="/updateCourseRate",method=RequestMethod.POST)
	@ResponseBody
	public JSONResult updateCourseRate(@RequestParam("data")String data){
		JSONResult rst = new JSONResult();
		rst.fail();
		CourseGradeRateView p=JSONUtils.json2pojo(data, CourseGradeRateView.class);
		if(p==null){
			return rst.fail("参数转化错误");
		}
		try{
			rst = this.genGradeService.updateCourseRate(p);
		}catch(Exception e){
			logger.error("出现异常"+e.getMessage());
			return rst.fail("后台出错");
		}
		return rst;
	}
	
	@RequestMapping("/getTaskList")
	@ResponseBody
	public JSONResult getTaskList(@RequestParam("courseId")int courseId){
		JSONResult rst = new JSONResult();
		rst.fail();
		IDParam p=new IDParam();
		p.setId(courseId);
		try{
			rst = this.genGradeService.getTaskList(p);
		}catch(Exception e){
			logger.error("出现异常"+e.getMessage());
			return rst.fail("后台出错");
		}
		return rst;
	}
	
	@RequestMapping(value="/updateTaskGradeStatus",method=RequestMethod.POST)
	@ResponseBody
	public JSONResult updateTaskGradeStatus(String data){
		JSONResult rst = new JSONResult();
		rst.fail();
		GenGradeServiceParam p=JSONUtils.json2pojo(data, GenGradeServiceParam.class);
		if(p==null){
			return rst.fail("参数转化错误");
		}
		try{
			rst = this.genGradeService.updateTaskGradeStatus(p);
		}catch(GenGradeException e){
			logger.error("出现异常"+e.getMessage());
			return rst.fail("后台出错");
		}catch(Exception e){
			logger.error("出现异常"+e.getMessage());
			return rst.fail("后台出错");
		}
		return rst;
	}
	
	@RequestMapping(value="/genTaskGrade",method=RequestMethod.POST)
	@ResponseBody
	public JSONResult genTaskGrade(@RequestParam("courseId")int courseId){
		JSONResult rst = new JSONResult();
		rst.fail();
		IDParam p=new IDParam();
		p.setId(courseId);
		try{
			rst = this.genGradeService.genTaskGrade(p);
		}catch(GenGradeException e){
			logger.error("出现异常"+e.getMessage());
			return rst.fail("后台出错");
		}catch(Exception e){
			logger.error("出现异常"+e.getMessage());
			return rst.fail("后台出错");
		}
		return rst;
	}
	
	@RequestMapping(value="/batchUpdateGrade",method=RequestMethod.POST)
	@ResponseBody
	public JSONResult batchUpdateGrade(@RequestParam("data")String data){
		JSONResult rst = new JSONResult();
		rst.fail();
		CalculateGradeParam.BatchUpdateGradeParam p=JSONUtils.json2pojo(data, CalculateGradeParam.BatchUpdateGradeParam.class);
		if(p==null){
			return rst.fail("参数转化错误");
		}
		try{
			rst = this.genGradeService.batchUpdateGrade(p);
		}catch(GenGradeException e){
			logger.error("出现异常"+e.getMessage());
			return rst.fail("后台出错");
		}catch(Exception e){
			logger.error("出现异常"+e.getMessage());
			return rst.fail("后台出错");
		}
		return rst;
	}
	
	@RequestMapping(value="/updateGrade",method=RequestMethod.POST)
	@ResponseBody
	public JSONResult updateGrade(@RequestParam("data")String data){
		JSONResult rst = new JSONResult();
		rst.fail();
		CalculateGradeParam.UpdateGradeParam p=JSONUtils.json2pojo(data, CalculateGradeParam.UpdateGradeParam.class);
		if(p==null){
			return rst.fail("参数转换错误");
		}
		
		try{
			rst = this.genGradeService.updateGrade(p);
		}catch(Exception e){
			logger.error("出现异常"+e.getMessage());
			return rst.fail("后台出错");
		}
		return rst;
	}
}

